package com.sparrow.utils;

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sparrow.constant.*;
import com.sparrow.core.Log;
public class JEMail {

	private String localAddress;

	public String getLocalAddress() {
		return localAddress;
	}

	public void setLocalAddress(String localAddress) {
		this.localAddress = localAddress;
	}

	private String host;
	private String from;
	private String to;
	private String subject;
	private String content;
	private boolean authentication = true;
	private String username;
	private String password;
	private String webSitName;

	public String getWebSitName() {
		return webSitName;
	}

	public void setWebSitName(String webSitName) {
		this.webSitName = webSitName;
	}

	public void sendMail(String toAddress, String subject, String content,
			String language) {
		String websitName = Config.getValueInFile(
				LANGUAGE_CONFIG_KEY.WEBSIT_NAME, language);

		this.setHost(Config.getValueInCache(CONFIG.EMAIL_HOST));
		this.setFrom(Config.getValueInCache(CONFIG.EMAIL_FROM));
		this
				.setUsername(Config
						.getValueInCache(CONFIG.EMAIL_USERNAME));
		this
				.setPassword(Config
						.getValueInCache(CONFIG.EMAIL_PASSWORD));
		this.setLocalAddress(Config
				.getValueInCache(CONFIG.EMAIL_LOCAL_ADDRESS));
		this.setAuthentication(true);
		content = content.replace("$websit_name", websitName);
		subject = subject.replace("$websit_name", websitName);
		this.setTo(toAddress);
		this.setSubject(subject);
		this.setContent(content);
		this.setWebSitName(websitName);

		this.sendMail();
	}

	/**
	 * 腾迅邮件需要手动开启smtp服务
	 * 
	 * @author zhanglizhi
	 * @date 2012-12-25
	 * @version 1.0
	 */
	public void sendMail() {
		try {
			Properties props = System.getProperties();
			props.put("mail.smtp.localhost", this.getLocalAddress());
			props.put("mail.smtp.host", this.getHost());
			Log.getInstance().info("mail.smtp.host:" + this.getHost());
			Log.getInstance().info("mail.smtp.host:" + this.getUsername());
			Log.getInstance().info("mail.smtp.host:" + this.getPassword());
			if (!authentication) {
				props.put("mail.smtp.auth", "false");
			} else {
				props.put("mail.smtp.auth", "true");
			}
			final String finalEmailUserName = this.getUsername();
			final String finalEmailPassword = this.getPassword();
			Session session = Session.getInstance(props, new Authenticator() {
				@Override
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(finalEmailUserName,
							finalEmailPassword);
				}
			});
			session.setDebug(true);
			MimeMessage message = new MimeMessage(session);
			InternetAddress fromAddress = new InternetAddress();
			fromAddress.setAddress(this.getFrom());
			fromAddress.setPersonal(this.getWebSitName());
			message.setFrom(fromAddress);
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(
					this.getTo()));
			message.setSubject(this.getSubject());
			message.setContent(this.getContent(), "text/html;charset=gb2312");
			message.saveChanges();
			if (authentication) {
				Transport transport = null;
				try {
					transport = session.getTransport("smtp");
					transport.connect(this.getHost(), this.getUsername(), this
							.getPassword());
					transport.sendMessage(message, message.getAllRecipients());
				} catch (Exception e) {
					Log.getInstance().error(e);
				} finally {
					transport.close();
				}
			} else {
				Transport.send(message);
			}
		} catch (Exception e) {
			Log.getInstance().error(e);
		}
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public boolean isAuthentication() {
		return authentication;
	}

	public void setAuthentication(boolean authentication) {
		this.authentication = authentication;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
